package com.getsendsocial.GetSendSocial.Util;

import java.util.concurrent.ExecutionException;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.actionbarsherlock.app.SherlockFragment;
import com.getsendsocial.GetSendSocial.R;
import com.getsendsocial.GetSendSocial.Database.GetString;
import com.getsendsocial.GetSendSocial.Activity.ABSTabsViewPagerActivity;
import com.getsendsocial.GetSendSocial.Activity.ContactActivity;
import com.getsendsocial.GetSendSocial.Activity.FacebookActivity;

public class GetTab extends SherlockFragment{
	public static String frase = "";

	 @Override
	 public View onCreateView(LayoutInflater inflater, ViewGroup container,
	   Bundle savedInstanceState) {
	   final View view = inflater.inflate(R.layout.get, null);
	   System.out.println(StartTab.flag);
	   
	   final TextView t = (TextView)view.findViewById(R.id.textViewPh);
	   final ImageView nextButton = (ImageView)view.findViewById(R.id.nextButton);
	   
	   final ImageView sendButton = (ImageView)view.findViewById(R.id.sendButton);
	   nextButton.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View arg0) {

						String cat = "";
					if(checkDb()==false){
						Toast.makeText(ABSTabsViewPagerActivity.c,
								"Devi sincronizzare il database per poter generare una nuova frase.", Toast.LENGTH_SHORT).show();
					}else{
					
						if(StartTab.flag != 0){
							
							if (StartTab.flag==1){
								cat="CongrTable";
							}
							if (StartTab.flag==2){
								cat="FunnyTable";
							}
							try {
								frase = new GetString(ABSTabsViewPagerActivity.c).execute(cat).get();
							} catch (InterruptedException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							} catch (ExecutionException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
							t.setText(frase);
					
						}else{
							Toast.makeText(ABSTabsViewPagerActivity.c, 
									"Devi selezionare una categoria nel tab Start!", Toast.LENGTH_SHORT).show();
						}
						}
			
					}
			});
		   
		   	sendButton.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
//					if (frase.equals("")|| frase==null){
//						Toast.makeText(ABSTabsViewPagerActivity.c, "Seleziona una frase valida", Toast.LENGTH_SHORT).show();
//					}else{
//					
//						Intent intent = new Intent(ABSTabsViewPagerActivity.c,ContactActivity.class);
//						startActivity(intent);	
//					}
//				}
//				});
					Intent intent = new Intent(ABSTabsViewPagerActivity.c,FacebookActivity.class);
					startActivity(intent);
				}	
				});

	   return view;	
}
	 
 @SuppressLint("SdCardPath")
private boolean checkDb(){
		 
		 SQLiteDatabase db = null;
			try {
		        db = SQLiteDatabase.openDatabase("/data/data/com.getsendsocial.GetSendSocial/databases/GSDb", null, SQLiteDatabase.OPEN_READONLY);
		        db.close();
		        return true;
		    } catch (SQLiteException e) {
		    	return false;
		    }
		 
	 }
}
